package cn.edu.mju.property.web.controller;

import cn.edu.mju.property.entity.Complaint;
import cn.edu.mju.property.service.ComplaintService;
import cn.edu.mju.property.service.impl.ComplaintServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/UpComplaint")
public class UpComplaint extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /*获取提交的数据*/
        Map<String,String[]> map = request.getParameterMap();
        Complaint complaint = new Complaint();
        try {
            BeanUtils.populate(complaint,map);
            ComplaintService service = new ComplaintServiceImpl();
            service.updateComplaint(complaint);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String id = request.getParameter("id");
        String st =  request.getParameter("status");
        String status = Boolean.parseBoolean(st) ?  "0" : "1";

        ComplaintService service = new ComplaintServiceImpl();
        boolean flag = service.upComplaintStatus(id,status);

        if(flag){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("error");
        }

    }
}
